import logging

from django.conf import settings
from django.core.mail import send_mail

from celery_tasks.main import app
from .yuntongxun.sms import CCP

logger = logging.getLogger("django")

# 验证码短信模板
SMS_CODE_TEMP_ID = 1


@app.task(name='send_sms_code')
def send_sms_code(mobile, sms_code):
    ccp = CCP()
    ccp.send_template_sms(mobile, [sms_code, '5'], 1)
    """
    发送短信验证码
    :param mobile: 手机号
    :param code: 验证码
    :param expires: 有效期
    :return: None
    """

    # try:
    #     ccp = CCP()
    #     result = ccp.send_template_sms(mobile, [code, expires], SMS_CODE_TEMP_ID)
    # except Exception as e:
    #     logger.error("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e))
    # else:
    #     if result == 0:
    #         logger.info("发送验证码短信[正常][ mobile: %s ]" % mobile)
    #     else:
    #         logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile)
    print('信息已发送...')


@app.task(name='send_email')
def send_email(validated_data, html_massage):
    send_mail('验证标题', '美多商城邮箱验证', settings.EMAIL_FROM,
              [validated_data['email']], html_message=html_massage)
